﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace A1
{
    /// <summary>
    /// 
    /// </summary>
    class PowerUp
    {

        private Rectangle rec; //The bounding rectangle
        private Paddle paddle; //The target power
        private Powers power; // The power this power up represents
        private int gameHeight; // The game height
        private int gameWidth; // the game width
        private Texture2D texture; //The texture of the power up
        private Rectangle textureRec; // The source rectangle for the texture on the sprite sheet
        /// <summary>
        /// The available power up types
        /// </summary>
        public enum Powers
        {
            Null,
            Enlarge,
            Shrink,
            Magnet,
            Push
        }

        /// <summary>
        /// Create a new power up
        /// </summary>
        /// <param name="paddle">the paddle that activated this powerup</param>
        /// <param name="power">The power this power up is</param>
        /// <param name="GameWidth">The width of the game</param>
        /// <param name="GameHeight">The height of the game</param>
        /// <param name="SpriteSheet">The spritee sheet of power ups</param>
        /// <param name="source">The rectangle from the power up block that created this</param>
        public PowerUp(Paddle paddle, Powers power, int GameWidth, int GameHeight, Texture2D SpriteSheet, Rectangle source)
        {
            this.paddle = paddle;
            this.power = power;
            gameWidth = GameWidth;
            gameHeight = GameHeight;
            texture = SpriteSheet;
            rec = new Rectangle(source.X, source.Y, source.Width, source.Height);
            textureRec = new Rectangle(50 * (int)power, 0, 50, 50);
        }

        /// <summary>
        /// Contains code that allows the power up to collide with a paddle
        /// </summary>
        /// <param name="gameTime">The game time</param>
        /// <returns>This power up instance, or null</returns>
        public PowerUp Update(GameTime gameTime)
        {
            Movement();
            if (paddle != null)
            {
                if (rec.Intersects(paddle.Rec))
                {
                    paddle.PreviousTime = gameTime.TotalGameTime;
                    paddle.Power = power;
                    //paddle.PowerActive = true;
                    paddle.NewPower = true;
                    return null;
                }
                if (rec.X < (0 - rec.Width) || rec.X > gameWidth || rec.Y < (0 - rec.Height) || rec.Y > (gameHeight))
                {
                    return null;
                }
            }
            return this;

        }

        /// <summary>
        /// Controls the power ups movements based off the target paddle
        /// </summary>
        private void Movement()
        {
            switch (paddle.Position)
            {
                case Game1.Position.Left:
                    rec.Offset(-5, 0);
                    break;
                case Game1.Position.Right:
                    rec.Offset(5, 0);
                    break;
                case Game1.Position.Top:
                    rec.Offset(0, -5);
                    break;
                case Game1.Position.Bottom:
                    rec.Offset(0, 5);
                    break;
            }
            //rec.Offset(5, 0);

        }

        /// <summary>
        /// Draw the paddle
        /// </summary>
        /// <param name="spriteBatch">The sprite batch object</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, rec, textureRec, Color.White);
        }
    }
}
